<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
    <style>
        .error{
            color: red;
            line-height: 20px;
        }

        .ztreeuldiv {
            height: 100%;
            width: 100%;
            background-color: white;
        }
        .layui-form-label {
            padding: 9px 15px !important;
            width: 50px !important;
        }
        .layui-form-item .layui-inline {
            margin-bottom: 5px;
            margin-right: 0px !important;
        }
    </style>
</head>
<body>
<div class="layui-fluid" id="flowDiv" onload="onLoad()">
    <div class="zTreeBackground left" style="float: left;width:400px">
        <div class="titlediv">
            <p style="text-align:right;background-color:white;line-height: 30px">
                组织管理<i class="layui-icon layui-icon-refresh" style="margin-left: 36%" onclick="refreshRoot()"></i>&ensp;&ensp;
            </p>

        </div>
        <div class="searchdiv" >
            <div class="layui-input-inline" style="width:100%">
                <input type="text" id="search" name="keyword" autocomplete="off" class="layui-input" placeholder="请输入名称"/>
                <i class="layui-icon layui-icon-search" style="position: absolute;top:8px;right: 8px;" onclick="searchByName()"></i>
            </div>
        </div>
        <div class="ztreediv" id="ztreediv">
            <div class="ztreeuldiv">
                <ul id="treeStructure" class="ztree"></ul>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var zTreeObj;
    var projectid;//点击当前项数据id
    var count=1,ids;
    $(function(){
        initTree("#treeStructure");
    })
    /**
     * 页面自适应
     */
    function getContentSize() {
        var wh = document.documentElement.clientHeight;
        var eh = 30;
        ch = (wh - eh) + "px";
        document.getElementById( "flowDiv" ).style.height = ch;
        document.getElementById( "ztreediv" ).style.height = (wh - eh - 50)+"px";
        document.body.style.height = (wh)+"px";
    }

    window.onload = getContentSize;
    window.onresize = getContentSize;
    var setting = {
        check: {
            enable: false,
            chkboxType: { "Y": "s", "N": "s" }
        },
        edit:{
            enable: false
        },
        async: {
            enable: true,
            url: publicUrl+"orgstation/getProjectsForTree",
            headers: {"Authorization":getData("Authorization")},
            dataFilter: formatedata,
            otherParam:{//传入查询参数
                //注意，要传入动态变化的参数必须用return的方式
                "personId":"3"
            },
            autoParam: ["id","type"]
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"id",
                pIdKey:"pid",
                isParent:"parent",
                rootPid:"",
                type:"type"
            },
            key: {
                isParent:"parent",
                url: "xUrl",
                icon:"xIcon",
                level:"orgLevel",
                name:"shortName"
            }
        },
        callback: {
            beforeClick: beforeClick,
            onClick: zTreeOnClick
        }
    };

    /**
     * 页面自适应
     */
    function getContentSize() {
        var wh = document.documentElement.clientHeight;
        var eh = 30;
        ch = (wh - eh) + "px";
        document.getElementById( "flowDiv" ).style.height = ch;
        document.getElementById( "ztreediv" ).style.height = (wh - eh - 50)+"px";
        document.body.style.height = (wh)+"px";
        $(".zTreeBackground.right").css("width",($(document).width()-350)+'px');//自适应表格处理
    }

    /**
     * @Author bxf
     * @Description 搜索框按钮
     * @Date 11:20 2019/7/17
     * @Param
     * @return
     **/
    function searchByName(){
        var search = $("#search").val();
        if(!search){
            return ;
        }
        loadTree("#treeStructure");
    }
    /**
     * @Author bxf
     * @Description 刷新左边树
     * @Date 11:20 2019/7/17
     * @Param
     * @return
     **/
    function refreshRoot(){
        $("#search").val("");
        initTree("#treeStructure");
    }

    //zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    function formatedata(treeId,parentNode,responseData) {
        return changeIcon(formatTreeParseData(treeId,parentNode,responseData,null));
    }

    function initTree(id){
        $(id).html('');
        publicAjax (null,'company/getByPersonId',{"personId":"3"},false,null,function(data){
            zTreeObj = $.fn.zTree.init($(id),setting, changeIcon(data.data));
        })
    }

    function loadTree(id){
        $(id).html('');
        publicAjax (null,'orgstation/getProjectsForTree',{"personId":"3","name":$("#search").val(),"stationFlag":1},false,null,function(data){
            zTreeObj = $.fn.zTree.init($(id),setting, changeIcon(data.data));
            zTreeObj.expandAll(true);
        })
    }

    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）

    function beforeClick(treeId, treeNode, clickFlag) {
        if(treeNode.type!=5){
            layer.msg("请选择项目！",{time:500});
            return false;
        }
    }


    function zTreeOnClick(event, treeId, treeNode, clickFlag) {
        nodeSelected = treeNode;
        parentNode = zTreeObj.getNodeByParam("id",treeNode.parentId,null);
        if(treeNode.type===5){
            projectid = treeNode.id;
            loadTable();
        }

    }
    function inintOrgstation(id){
        ids = id;
    }

    function submit(){
        var nodes = zTreeObj.getSelectedNodes()[0];
        if(!nodes){
            layself("请选择项目");
            return false;
        }
        var result;
        publicAjax (null,'orgstation/moveOrgStation',{'ids':ids,'projectId':projectid},false,null,function (r) {
            if(r.code==2000){
                laysuccess("移动成功");
                result = r;
            }else{
                layerror("移动失败");
                result = r;
            }
        })
        return result;
    }

</script>
</html>